package com.situ.mall.interceptor;

import com.situ.mall.pojo.Admin;
import com.situ.mall.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.判断用户有没有登录
        //2.如果登录了，就放行，可以访问后台的资源
        //3.如果没有登录，跳转到登录界面
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            // /page/cart/list
            // /page/order/list
            String requestURI = request.getRequestURI();
            //没有登录，跳转到登录界面
            response.sendRedirect("/page/login?returnURI=" + requestURI);
            return false;
        }

        return true;
    }
}
